package test.check.system;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class CheckSystem {
	public void checkNetwork() {
		if(System.getProperty("os.name").toLowerCase().contains("windows")) {
			runCommand("netstat -an | findstr 127");
		}else {
			runCommand("netstat -an | grep 127");
		}
	}
	
	public void checkStorageAvailablity() {
		if(System.getProperty("os.name").toLowerCase().contains("windows")) {
			//runCommand("");
		}else {
			runCommand("df -h");
		}
	}
	
	public void checkServerProcess() {
		if(System.getProperty("os.name").toLowerCase().contains("windows")) {
			runCommand("tasklist | findstr java");
		}else {
			runCommand("ps -ef | grep admin");
		}
	}
	
	public String runCommand(String command) {
		if(System.getProperty("os.name").toLowerCase().contains("windows")) {
			return runCommandOnWindows(command);
		}else {
			StringBuilder sb = new StringBuilder();
			sb.append("bash -c \"").append(command).append("\"");
			String commandAsArg = sb.toString();
			String commandResult = null;
			
			Runtime runtime = Runtime.getRuntime();
			try {
				Process proc = runtime.exec(new String[]{"bash", "-c", commandAsArg});
				int exitCode = proc.waitFor();
				
				if(exitCode == 0) {
					System.out.println("Command \"" + command + "\" run successfully.");
					InputStream is = proc.getInputStream();
					BufferedReader br = new BufferedReader(new InputStreamReader(is));
					String line = null;
					sb = new StringBuilder();
					while((line = br.readLine()) != null) {
						System.out.println(line);
						sb.append(line).append("\n");
					}
					commandResult = sb.toString();
					is.close();
				} else {
					System.out.println("Command failed with error code " + exitCode);
					InputStream is = proc.getErrorStream();
					BufferedReader br = new BufferedReader(new InputStreamReader(is));
					String line = null;
					sb = new StringBuilder();
					while((line = br.readLine()) != null) {
						System.out.println(line);
						sb.append(line).append("\n");
					}
					commandResult = sb.toString();
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			return commandResult;
		}
	}
	
	public String runCommandOnWindows(String command) {	
		StringBuilder sb = new StringBuilder();
		String commandResult = null;
		
		Runtime runtime = Runtime.getRuntime();
		try {
			Process proc = runtime.exec(new String[]{"cmd", "/c", command});
			int exitCode = proc.waitFor();
			
			if(exitCode == 0) {
				System.out.println("Command \"" + command + "\" run successfully.");
				InputStream is = proc.getInputStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				String line = null;
				sb = new StringBuilder();
				while((line = br.readLine()) != null) {
					System.out.println(line);
					sb.append(line).append("\n");
				}
				commandResult = sb.toString();
				is.close();
			} else {
				System.out.println("Command failed with error code " + exitCode);
				InputStream is = proc.getErrorStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				String line = null;
				sb = new StringBuilder();
				while((line = br.readLine()) != null) {
					System.out.println(line);
					sb.append(line).append("\n");
				}
				commandResult = sb.toString();
				is.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		return commandResult;
	}
}
